<template>
  <div id="home" >
    <nav-bar class="home-nav">
      <div slot="center">购物车</div>
    </nav-bar>

    <tab-control ref="PlaceHolderTabControl"
                 :titles="['流行','新款','精选']"
                 @tabClick="tabClick"
                  v-show="isTabFixed"/>

    <scroll class="content" ref="scroll"
            :probe-type="3"
            :pull-up-load="true"
            @scrollPostion="scrollPostion"
            @pullingUp="loadMore">

      <!--    <home-swiper :banners="banners"/>-->
      <home-swiper-github :banners="banners" @swiperImageLoad="swiperImageLoad"/>
      <recommend-view :recommends="recommends"  />
      <feature-view/>
      <tab-control ref="tabControl"
                   :titles="['流行','新款','精选']"
                   @tabClick="tabClick"/>

      <goods-list :goods="showGoods"/>
    </scroll>

    <back-top @click.native="backClick" v-show="isShowBackTop"/>

  </div>
</template>

<script>
  import NavBar from 'components/common/navbar/NavBar'
  import Scroll from "components/common/scroll/Scroll";

  import TabControl from 'components/content/tabControl/TabControl'
  import GoodsList from "components/content/goods/GoodsList";
  import BackTop from "components/content/backTop/BackTop";

  import HomeSwiper from "./childComps/HomeSwiper";
  import HomeSwiperGithub from "./childComps/HomeSwiperGithub";
  import RecommendView from './childComps/RecommendView'
  import FeatureView from "./childComps/FeatureView";

  import '../../mock/index'

  import {debounce} from "common/utils";
  import {
    getHomeMultidata,
    getMockMutidata,
    getHomeGoods,
    getMockHomeGoods
  } from "network/home";

  export default {
    name: "Home",
    components: {
      NavBar,
      HomeSwiper,
      HomeSwiperGithub,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll,
      BackTop
    },
    data() {
      return {
        banners: [],
        recommends: [],
        //首页数据模型
        goods: {
          'pop': {page: 0, list: []},
          'new': {page: 0, list: []},
          'sell': {page: 0, list: []}
        },
        currentType: 'pop',
        isShowBackTop:false,
        tabOffsetTop:0,   //tabControl距离顶部的值
        isTabFixed:false,   //tabControl是否吸顶
        saveY:0
      }
    },
    computed:{
      showGoods(){
        return this.goods[this.currentType].list;
      }
    },
    created() {
      //获取首页的各种信息
      this.mGetHomeMultidata();
      //获取首页的详细商品信息
      this.mGetHomeGoods('pop');
      this.mGetHomeGoods('new');
      this.mGetHomeGoods('sell');
    },
    mounted() {
      // 1.图片加载完成的事件监听
      const refresh = debounce(this.$refs.scroll.refresh, 50)
      this.$bus.$on('itemImageLoad', () => {
        refresh()
      })

    },
    destroyed() {
      console.log('销毁了');
    },
    activated() {       //进入时,直接滚到Y位置
      this.$refs.scroll.scrollTo(0,this.saveY,0);
      this.$refs.scroll.refresh()
    },
    deactivated() {   //离开时记住y值
      this.saveY = this.$refs.scroll.getScrollY();
    },
    methods: {

      mGetHomeMultidata() {
        getHomeMultidata().then(res => {
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        })

        //mock出来的数据,防止接口不让用了
        // getMockMutidata().then(res=>{
        //   console.log(res)
        // })
      },
      mGetHomeGoods(type) {
        //保留上一次页数
        const page = this.goods[type].page + 1;
        getHomeGoods(type, page).then(res => {
          // console.log(res.data.list);
          this.goods[type].list.push(...res.data.list);
          this.goods[type].page += 1;

          //结束上拉加载更多
          this.$refs.scroll.finishPullUp();
        })
      },
      /**
       * tabControl事件的切换
       */
      tabClick(index) {
        switch (index) {
          case 0:
            this.currentType = 'pop';
            break;
          case 1:
            this.currentType = 'new';
            break;
          case 2:
            this.currentType='sell';
            break
        }
        this.$refs.tabControl.currentIndex = index;
        this.$refs.PlaceHolderTabControl.currentIndex = index;
      },
      backClick(){
        // this.$refs.scroll.scroll.scrollTo(0,0,500)
        this.$refs.scroll.scrollTo(0,0)
      },
      /**
       * 监听滚动
       * @param position
       */
      scrollPostion(position){
        // backTop是否显示
        this.isShowBackTop=position.y<-1000
        // 决定是否 tabControl是否吸顶
        this.isTabFixed=(-position.y) > this.tabOffsetTop
      },
      loadMore(){
        this.mGetHomeGoods(this.currentType);
      },
      swiperImageLoad(){
        //所有的vue组件都有一个$el,用于获取组件中的元素
        this.tabOffsetTop=this.$refs.tabControl.$el.offsetTop;
        console.log(this.tabOffsetTop);
      }
    }

  }
</script>

<style scoped>
  #home {
    /*padding-top: 44px;*/
    height: 100vh;
    position: relative;
  }

  .home-nav {
    background-color: var(--color-tint);
    color: #fff;


  }

  /*.tab-control {*/
  /*  position: sticky;*/
  /*  top: 44px;*/
  /*  z-index: 9;*/
  /*}*/

  .content {
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }

  .fixed{
    position: fixed;
    left:0;
    right: 0;
    top:44px
  }

  .tab-control {
    position: relative;
    z-index: 9;
  }
  /*.content {*/
  /*height: calc(100% - 93px);*/
  /*overflow: hidden;*/
  /*margin-top: 44px;*/
  /*}*/

</style>

